package com.fingard.dsp.bank.directbank.citic02;

import com.fingard.dsp.bank.directbank.bankInterface.IMailedMsg;
import com.fingard.dsp.bank.reqrespobj.mailedmsg.ReqMailedMsgQry;
import com.fingard.dsp.bank.reqrespobj.mailedmsg.RetAtsMailedMsg;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/29.
 */
public class CITIC02MailedMsg extends CITIC02Base implements IMailedMsg {
    @Override
    public RetAtsMailedMsg getMailedMsg(ReqMailedMsgQry qry) throws Exception {
        RetAtsMailedMsg retAts = new RetAtsMailedMsg(qry.reqHeader);
        retAts.writeStart();
        try {
            CITIC02Header header =  new CITIC02Header();
            header.stdprocode = "RLTMSG";
            header.msgsendertrc = reqHeader.transSeqID;
            header.trdcode = "CPCDRGSM";
//            封装body内容
            XmlTextWriter xmlWriterBody = new XmlTextWriter();
            xmlWriterBody.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xmlWriterBody.writeStartElement("stream");
//            ORGCODE	组织机构代码	VARCHAR	12	N
            xmlWriterBody.writeElementString("ORGCODE","");
//            SERIALNO	流水号	VARCHAR	20	Y	企业端交易流水号，控制不能重复，字母+YYYYMMDD+7位流水
            xmlWriterBody.writeElementString("SERIALNO",reqHeader.transSeqID);
//            LONCPID	企业端经销商客户代码	VARCHAR	20	Y	企业系统内部的经销商编号
            xmlWriterBody.writeElementString("LONCPID",qry.DealerCode);
//            AGTNO	协议编号	VARCHAR	60	N
            xmlWriterBody.writeElementString("AGTNO","");
//            BIZMOD	业务模式	VARCHAR	20	N	11 先票后货三方 12 先票后货两方 13 保兑仓
            xmlWriterBody.writeElementString("BIZMOD","");
//            SCFTXNO	放款批次号	VARCHAR	20	N
            xmlWriterBody.writeElementString("SCFTXNO","");
//            CDTPROD	授信产品	VARCHAR	20	N	1 银行承兑汇票；2 流动资金贷款；3 法人账户透支；4 商票贴现；5 国内信用证；6 进口信用证；7 非信用证项下进口押汇
            xmlWriterBody.writeElementString("CDTPROD","");
//            CARR	承运人	VARCHAR	122	N
            xmlWriterBody.writeElementString("CARR","");
//            DEST	目的地	VARCHAR	256	N
            xmlWriterBody.writeElementString("DEST","");
//            EXPARRDT	预计到货日期	DATE		N
            xmlWriterBody.writeElementString("EXPARRDT","");
//            FIELD1	属性1(发货状态)	VARCHAR	30	Y	1 发货 2退货(长安暂不实现) 0收货(长安暂不实现)
            xmlWriterBody.writeElementString("FIELD1","1");
//            FIELD2	属性2(品牌)	VARCHAR	60	N
            xmlWriterBody.writeElementString("FIELD2","");
//            FIELD3	属性3	VARCHAR	100	N
            xmlWriterBody.writeElementString("FIELD3","");
//            FIELD13	接口类型	VARCHAR	2	N
            xmlWriterBody.writeElementString("FIELD13","");
//            LIST	汽车列表	LIST
            xmlWriterBody.writeStartElement("LST");

            while (qry.readRecord()) {
                String[] billArr = qry.ReqDetail.BillNo.split(",");
                for(String bill:billArr) {
                    xmlWriterBody.writeStartElement("row");
//                FIELD4	属性4	VARCHAR	30	Y	车架号
                    xmlWriterBody.writeElementString("FIELD4", qry.ReqDetail.VIN);
//                FIELD5	属性5	DECIMAL	19,2	Y	车价
                    xmlWriterBody.writeElementString("FIELD5", qry.ReqDetail.PickupPrice);
//                FIELD6	属性6	VARCHAR	10	Y	汽车状态：1发货，2退货(长安暂不实现)，0到达(长安暂不实现)
                    xmlWriterBody.writeElementString("FIELD6", "1");
//                FIELD7	属性7	VARCHAR	30	Y	发动机号
                    xmlWriterBody.writeElementString("FIELD7", qry.ReqDetail.EngineNo);
//                BRAND	品牌	VARCHAR	40	N	品牌 （修改为非必输）
                    xmlWriterBody.writeElementString("BRAND", qry.ReqDetail.CarTypeCode);
//                FIELD8	属性8	VARCHAR	100	Y	车型
                    xmlWriterBody.writeElementString("FIELD8", qry.ReqDetail.MyCode);
//                FIELD9	属性9	VARCHAR	30	N	款式
                    xmlWriterBody.writeElementString("FIELD9", "");
//                FIELD10	属性10	VARCHAR	60	N	配置
                    xmlWriterBody.writeElementString("FIELD10", "");
//                HGZNO	合格证编号	VARCHAR	60	Y	合格证编号
                    xmlWriterBody.writeElementString("HGZNO", qry.ReqDetail.CertificationNo);
//                FIELD11	属性11	VARCHAR	100	Y	融资编号（票号） 新增逻辑处理支持票号后8位模糊查询
                    xmlWriterBody.writeElementString("FIELD11",bill);
//                FIELD12	属性12	VARCHAR	100	N	企业端融资申请编号（厂商订货计划编号）
                    xmlWriterBody.writeElementString("FIELD12", "");
//                FIELD15	属性15	VARCHAR	30	N
                    xmlWriterBody.writeElementString("FIELD15", "");
//                FIELD16	属性16	VARCHAR	8	N
                    xmlWriterBody.writeElementString("FIELD16", "");
//                FIELD17	属性17	VARCHAR	15,2	N
                    xmlWriterBody.writeElementString("FIELD17", "");
//                FIELD18	属性18	VARCHAR	60	N
                    xmlWriterBody.writeElementString("FIELD18", "");
//                FIELD19	属性19	VARCHAR	4	N
                    xmlWriterBody.writeElementString("FIELD19", "");
//                FIELD20	属性20	VARCHAR	30	N
                    xmlWriterBody.writeElementString("FIELD20", "");
                    xmlWriterBody.writeEndElement();
                }
            }
//                    LIST_END
            xmlWriterBody.writeEndDocument();
            String[] tmpStrRet = sendToBank2(xmlWriterBody.toXmlString(),header);
            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String retCode =tmpBkReader.getFirstText("/stream/RETCODE");
                    if("AAAAAAA".equals(retCode)){
                        retAts.respCode = "0";
                    }else{
                        retAts.respCode = "-1";
                    }
                    retAts.respInfo = retCode;
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.getStackTrace();
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
        }
        retAts.writeRespCode(retAts.respCode);
        retAts.writeRespInfo(retAts.respInfo);
        retAts.writeEnd();
        return retAts;
    }
}
